﻿
Public Class frmProveedorListado

    Private Sub frmProveedorListado_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgProveedor.AutoGenerateColumns = False
        CargarProveedores()
    End Sub

    Dim controller As New ControllerSigeco.controller_compras()

    Private m_frmProveedor As frmProveedor
    Public Property frmProveedores() As frmProveedor
        Get
            Return m_frmProveedor
        End Get
        Set(ByVal value As frmProveedor)
            m_frmProveedor = value
        End Set
    End Property

    Private Sub CargarProveedores()

        Dim dt As New DataTable
        dt = controller.get_Proveedores_All(oiml.dbConn, 0, txtProveedor.Text, txtruc.Text, Not cbxHabilitado.Checked)
        dgProveedor.DataSource = dt

    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()

    End Sub

    Private Sub btnEditar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEditar.Click
        Try
            If dgProveedor.SelectedRows.Count > 0 Then
                Dim cod As New Integer
                cod = dgProveedor.SelectedRows(0).Cells(0).Value

                If frmProveedores Is Nothing Then

                    frmProveedores = New frmProveedor
                    frmProveedores.codProveedor = cod

                    'If frmClientes.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    frmProveedores.ShowDialog()
                    CargarProveedores()
                    frmProveedores.Dispose()
                    frmProveedores = Nothing
                    'End If
                Else
                    frmProveedores.codProveedor = cod
                    frmProveedores.ShowDialog()
                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.Click
        CargarProveedores()
    End Sub

    Private Sub txtProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtProveedor.KeyDown, txtruc.KeyDown, cbxHabilitado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarProveedores()

        End If
    End Sub

    Private Sub dgProveedor_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgProveedor.DoubleClick
        btnEditar_Click(sender, e)
    End Sub

    Private Sub dgProveedor_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgProveedor.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnEditar_Click(sender, e)
        End If

    End Sub

    Private Sub btnNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNuevo.Click
        frmProveedores = New frmProveedor()
        frmProveedores.codProveedor = 0
        frmProveedores.ShowDialog()
        CargarProveedores()
    End Sub

    Private m_frmReporte As frmReportes
    Public Property frmRpts() As frmReportes
        Get
            Return m_frmReporte
        End Get
        Set(ByVal value As frmReportes)
            m_frmReporte = value
        End Set
    End Property

    Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click

        If frmRpts Is Nothing Then
            frmRpts = New frmReportes
            'frmRpts.MdiParent = Me
            frmRpts.tipo_Reporte = "PROVEEDORES"
            frmRpts.datos_Reporte = CType(dgProveedor.DataSource, DataTable).Copy
            frmRpts.ShowDialog()
        Else

            If Not frmRpts.Visible Then
                frmRpts = New frmReportes()
                'frmRpts.MdiParent = Me
                frmRpts.tipo_Reporte = "PROVEEDORES"
                frmRpts.datos_Reporte = CType(dgProveedor.DataSource, DataTable).Copy
                frmRpts.ShowDialog()
            Else
                frmRpts.tipo_Reporte = "PROVEEDORES"
                frmRpts.datos_Reporte = CType(dgProveedor.DataSource, DataTable).Copy
                frmRpts.Select()
                frmRpts.Activate()
            End If

        End If

    End Sub

End Class